﻿
Partial Class App_Categorize
    Inherits MyUI.PageBaseApp

    'Dim WithEvents d As New SimilText.Tools.Dictionary

    Protected SortGridLocations As UI.SortControl
    Public CurrAccount As mDB.Account
    Dim FolderParentId As Integer


    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load



        SortGridLocations = New UI.SortControl(Me.GridUsers.ID, Me.ViewState, "Accuracy", SortDirection.Descending)

        CurrAccount = CurrUser.CurrAccount


        'Me.Items("RootFolderID") = -1
        'Me.Items("tree_type_id") = CInt(mDB.Folder.GetFolderType(mDB.Folder.DataType.CATEGORIZE, CurrAccount.Network_Id))
        Dim FolderType = mDB.Folder.GetFolderType(mDB.Folder.DataType.CATEGORIZE, CurrAccount.Network_Id)
        Me.JsVar("root_folder_id") = CurrUser.Tab_Folder(FolderType, 0).ToObj.Id
        Me.JsVar("tree_type_id") = CInt(FolderType)
        Me.JsVar("webservice_url") = Me.ResolveClientUrl("~/Webservice/Folder.asmx/")
        Me.JsVar("icons_path") = Me.ResolveClientUrl("~/Style/App/img/Icons/")

        FolderParentId = Utils.Conv.ToInt(Me.HidSelectFolderId.Value) ' Me.HidSelectFolderId.Value

        If Not Me.IsPostBack Then
            SetDataPage()
        Else

        End If


    End Sub

    Public Sub SetDataPage()

        SetListAccountFollowQuery()
        SetListCategoryWiz()

    End Sub

    '#Region "ACCOUNT"

    '    Protected Sub AccountSelect_SelectedChanged() Handles AccountSelect.SelectedChanged

    '        SetDataPage()

    '    End Sub

    '#End Region

#Region "WIZARD"

    Public Sub SetListCategoryWiz()

        Dim root = DB.TAB.Folder.BY.Folder.User_Id(-1, GloukDB.Folder.Type.TWITTER_CATEGORIZE, 0).ToObj

        Dim dt = DB.TAB.Folder.BY.Folder.
            User_Id(-1, GloukDB.Folder.Type.TWITTER_CATEGORIZE, root.Id).ToList

        UI.ListData.Bind(Me.ListCatWiz, dt, {"id"})


    End Sub

    Protected Sub ListCatWiz_ItemCommand(sender As Object, e As ListViewCommandEventArgs) 'Handles ListCatWiz.ItemCommand
        Dim list As ListView = sender

        Dim id = UI.ListDataItem.GetKey(e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        'Dim query = DB.TAB.Categorize.GetByPK(id)

        Dim b = UI.ListDataItem.GetLinkButton(Row, "BtnCat")

        UI.Controls.ClassReplace(b, "bottone-start-list", "bottone-start-list-active")

        'bottone-start-list-active


        Select Case e.CommandName

 
        End Select
    End Sub

    Protected Sub ListCatWiz_ItemCommand2(sender As Object, e As System.EventArgs) 'Handles ListCatWiz.ItemCommand
        Dim list As ListView = sender


    End Sub


#End Region


#Region "CATEGORIZE"

    'Public ParentFold As mDB.Folder



    Public Sub SetListQuery(AddRow As Boolean)
        Dim List = Me.ListQuery
        'Dim ParentId = Me.HidSelectFolderId.Value
        Dim dt As DataTable

        If Me.FolderParentId = 0 Then Exit Sub

        'QUERY
        'ParentFold = DB.TAB.Folder.GetByPK(Me.FolderParentId)

        'If ParentFold.User_Id = -1 Then
        '    dt = DB.TAB.Categorize.
        '        REL.Categorize.Folder.
        '        BY.Folder.Ref_Id(Me.FolderParentId).
        '        BY.Folder.User_Id(-1).
        '    ToDataTable
        'Else
        '    'dt = CurrUser.Tab_Query(ParentId).ToDataTable
        '    dt = CurrUser.Tab_Categorize(Me.FolderParentId).ToDataTable

        'End If

        dt = CurrUser.Tab_Categorize(Me.FolderParentId).ToDataTable

        If AddRow Then
            Utils.DT.AddDataRowAt(dt, UI.ListData.GetIndexData(List, 0))
            'dt.Insert(UI.ListData.GetIndexData(List, 0), QueryDef)
            List.EditIndex = 0
        End If

        'categori selezionate
        Dim QueryFoll = CurrAccount.Tab_Categorize_Follower.ToDictionary(Of Integer)("categorize_id")

        '-------------------------------------------------------
        ' LAYOUT
        '-------------------------------------------------------

        Dim DataBind = UI.ListData.Bind(List, dt, {"id"})

        'ADD
        'If AddRow Then
        '    Utils.DT.AddDataRowAt(dt, Me.GridElaborations.Rows(0).DataItemIndex)
        '    Me.GridElaborations.EditIndex = 0
        '    Me.GridElaborations.SelectedIndex = 0

        'End If

        ''-------------------------------------------------------
        '' LAYOUT
        ''-------------------------------------------------------

        'UI.Grid.Bind(g, dt, {"id"})

        'g.Columns(3).Visible = ParentFold.User_Id > 0

        'For Each gr As GridViewRow In g.Rows
        '    Dim query = dt(UI.GridRow.GetIndexData(gr))
        '    Dim Follow = UI.GridRow.GetControl(Of ImageButton)(gr, "IbtFollow")
        '    'Dim UnFollow = UI.GridRow.GetControl(Of ImageButton)(gr, "IbtUnFollow")
        '    If Follow IsNot Nothing Then
        '        Follow.Visible = Not QueryFoll.ContainsKey(query("Id"))
        '    End If
        '    'UnFollow.Visible = Not Follow.Visible

        'Next

        Me.UpPEnquiry.Update()

    End Sub
    Protected Sub ListQuery_ItemCommand(sender As Object, e As ListViewCommandEventArgs) Handles ListQuery.ItemCommand
        Dim list As ListView = sender

        Dim id = UI.ListDataItem.GetKey(e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim Row = UI.ListDataItem.GetRow(e.CommandSource)

        Dim query = DB.TAB.Categorize.GetByPK(id)

        Select Case e.CommandName

            Case "CANCEL_ROW_EDIT"

                list.EditIndex = -1
                SetListQuery(False)

            Case "UPDATE_ROW"

                If query Is Nothing Then
                    query = (New mDB.Categorize With {.Folder_Id = Me.FolderParentId})
                    DB.TAB.Categorize.InsertOnSubmit(query)
                End If

                query.Query = UI.ListDataItem.GetTextBox(Row, "TxtQuery").Text
                query.Description = UI.ListDataItem.GetTextBox(Row, "TxtDescription").Text

                DB.SubmitChanges()

                list.EditIndex = -1
                SetListQuery(False)

            Case "EDIT_ROW"
                list.EditIndex = Row.DisplayIndex
                SetListQuery(False)

            Case "DELETE_ROW"
                DB.TAB.Categorize.DeleteOnSubmit(query)
                DB.SubmitChanges()
                SetListQuery(False)


            Case "ANALIZE_EDIT"

                SetDataSearch(UI.ListDataItem.GetTextBox(Row, "TxtQuery").Text)

            Case "ANALIZE"

                SetDataSearch(query.Query)
                'SetDataSearchQuery(New List(Of mDB.Query) From {query}, "")

            Case "DOWNLOAD"
                'query.ReIndex()
                'DB.SubmitChanges()
                'SetListQuery(False)


        End Select
    End Sub

    Protected Sub BtnAddQuery_Click(sender As Object, e As System.EventArgs) Handles BtnAddQuery.Click

        If Not MyApp.GetUserLimitPlan(GloukDB.Plan.LimitType.QUERY_PER_USER, True) Then
            SetListQuery(True)
        End If

    End Sub
    'Protected Sub GridElaborations_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridElaborations.PageIndexChanging

    '    GridElaborations.PageIndex = e.NewPageIndex

    '    SetListQuery(False)

    'End Sub
    'Protected Sub GridElaborations_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridElaborations.RowCommand
    '    Dim g As GridView = sender

    '    Dim id = UI.Grid.GetKey(sender, e.CommandSource)

    '    If id Is Nothing Then
    '        Exit Sub
    '    End If

    '    Dim gr = UI.GridRow.GetRow(e.CommandSource)

    '    Dim query = DB.TAB.Categorize.GetByPK(id)

    '    Select Case e.CommandName
    '        Case "EDIT_ROW"
    '            g.EditIndex = gr.RowIndex
    '            SetListQuery(False)

    '        Case "CANCEL_ROW_EDIT"

    '            g.EditIndex = -1
    '            SetListQuery(False)

    '        Case "UPDATE_ROW"

    '            If query Is Nothing Then
    '                query = (New mDB.Categorize With {.Folder_Id = Me.FolderParentId}) '.SetDefault
    '                DB.TAB.Categorize.InsertOnSubmit(query)
    '            End If

    '            query.Query = UI.GridRow.GetTextBox(gr, "TxtQuery").Text
    '            'query.Place = UI.GridRow.GetTextBox(gr, "TxtPlace").Text
    '            'query.Lang_Code = UI.GridRow.GetTextBox(gr, "TxtLangCode").Text
    '            query.Description = UI.GridRow.GetTextBox(gr, "TxtDescription").Text

    '            DB.SubmitChanges()

    '            g.EditIndex = -1
    '            SetListQuery(False)

    '        Case "DELETE_ROW"
    '            DB.TAB.Categorize.DeleteOnSubmit(query)
    '            DB.SubmitChanges()
    '            SetListQuery(False)
    '        Case "LOAD"

    '            g.SelectedIndex = gr.RowIndex


    '            Me.Txtpost.Text = query.Query

    '            Me.UpPtext.Update()

    '        Case "FOLLOW"

    '            Dim QueryFoll = CurrAccount.Tab_Categorize_Follower(query.Id).ToObj

    '            If QueryFoll Is Nothing Then
    '                QueryFoll = New mDB.Categorize_Follower With {
    '                    .Account_Id = CurrAccount.Id, .Categorize_Id = query.Id, .Accuracy_Total_Account = 1000
    '                }
    '                DB.TAB.Categorize_Follower.InsertOnSubmit(QueryFoll)
    '                DB.SubmitChanges()
    '            End If

    '            Me.SetListQuery(False)
    '            Me.SetListAccountFollowQuery()

    '    End Select

    'End Sub
    'Protected Sub BtnReload_UpPElaborations_Click(sender As Object, e As System.EventArgs) Handles BtnReload_UpPElaborations.Click

    '    SetListQuery(False)

    'End Sub

    Protected Sub BtnReload_UpPEnquiry_Click(sender As Object, e As System.EventArgs) Handles BtnReload_UpPEnquiry.Click

        SetListQuery(False)

    End Sub
    'Protected Sub BtnSaveNew_Click(sender As Object, e As System.EventArgs) Handles BtnSaveNew.Click

    '    If Me.FolderParentId <> 0 Then
    '        Dim Cat = New mDB.Categorize With {
    '            .Description = Me.TxtDescription.Text, .Folder_Id = Me.FolderParentId, .Query = Me.Txtpost.Text}

    '        DB.TAB.Categorize.InsertOnSubmit(Cat)

    '        If Cat.SubmitChanges() Then
    '            LblMessCat.Text = "Categoria creata"
    '        Else
    '            LblMessCat.Text = "Categoria già esistente"
    '        End If

    '        SetListQuery(False)

    '    Else
    '        LblMessCat.Text = "Selzionare una cartella"

    '        'LblMessCat.Text = ""

    '    End If

    'End Sub

#End Region

#Region "CATEGORIZE FOLLOWER"

    Public Sub SetListAccountFollowQuery()
        Dim g = Me.GridFollowQuery


        Dim dt = CurrAccount.Tab_Categorize_Follower.
            REL.Categorize_Follower.Categorize.
        ToDataTable()

        '            SetSelect("categorize.id", "query", "description", "accuracy_total_account").

        g.PageSize = 30
        UI.Grid.Bind(g, dt, {"id"})

        Me.UpPFollowQuery.Update()

    End Sub

    Protected Sub GridFollowQuery_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridFollowQuery.RowCommand
        Dim g As GridView = sender

        Dim id = UI.Grid.GetKey(g, e.CommandSource)

        If id Is Nothing Then
            Exit Sub
        End If

        Dim gr = UI.GridRow.GetRow(e.CommandSource)

        Dim query = DB.TAB.Categorize.GetByPK(id)


        Select Case e.CommandName

            Case "UNFOLLOW"

                CurrAccount.Tab_Categorize_Follower(query.Id).Delete.ExecuteCommand()
                Me.SetListQuery(False)
                Me.SetListAccountFollowQuery()

            Case "CATEGORIZE"

                Dim FollCat = CurrAccount.Tab_Categorize_Follower(query.Id).ToObj

                MyApp.Task.Elaborate.CategorizeFollowersAccountCategory(FollCat)

                Me.SetListAccountFollowQuery()




        End Select
    End Sub
    Protected Sub GridFollowQuery_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridFollowQuery.PageIndexChanging

        GridFollowQuery.PageIndex = e.NewPageIndex

        Me.SetListAccountFollowQuery()
    End Sub

    Protected Sub BtnUpdateFollQuery_Click(sender As Object, e As System.EventArgs) Handles BtnUpdateFollQuery.Click
        Dim g = Me.GridFollowQuery

        Dim RecEditID = (From k In g.DataKeys Select k.valUe).ToList
        If RecEditID.Count > 0 Then

            Dim Records = CurrAccount.Tab_Categorize_Follower.Where("categorize_Id", "in", RecEditID).ToDictionary(Of Integer)("categorize_Id")
            For ct As Integer = 0 To g.Rows.Count - 1

                Records(g.DataKeys(ct).Value).Accuracy_Total_Account = Utils.Conv.ToDouble(UI.Grid.GetTextBox(g, ct, "TxtAccuracyTotalAccount").Text)

            Next

            DB.SubmitChanges()

        End If

        Me.SetListAccountFollowQuery()


    End Sub


#End Region

#Region "ANALISYS"

    Public Sub SetDataSearch(text As String)

        '----------------------------------------------------------
        'RICERCA
        '----------------------------------------------------------
        Dim AD = mDB.Archive_Simil_Text.GetArchive(mDB.Archive_Simil_Text.ArchiveType.POST_FOLLOWERS, CurrAccount.Id).GetSimilTextArchiveData

        'AD.Rank_word_word = Utils.Conv.ToInt(Me.TxtWordWord.Text)
        'AD.Rank_Hashtag_hashtag = Utils.Conv.ToInt(Me.Txthashhash.Text)
        'AD.Rank_word_hashtag = Utils.Conv.ToInt(Me.TxtWordhash.Text)
        'AD.Rank_Hashtag_word = Utils.Conv.ToInt(Me.TxthashWord.Text)
        'AD.Rank_stemming = Utils.Conv.ToInt(Me.Txtstemm.Text)
        'AD.Rank_word_prox = Utils.Conv.ToInt(Me.Txtprox.Text)

        Dim f As New SimilText.FindSimilarText(AD)

        f.Text = text
        f.RankMin = 70 'Utils.Conv.ToInt(Me.TxtAcc.Text)
        f.Find()
        f.Close()

        Me.LblExec.Text = String.Format("Analisi effettuata in {0} sec.", f.Result.ExecTime)


        '----------------------------------------------------------
        ' TROVA GLI ACCOUNT CON MAGGIOR ACCURACY
        '----------------------------------------------------------

        Dim Accounts = (
            From r In f.Result.Matches
            Group r By r.Attribute0 Into g = Group
            Select id = Attribute0, AccTot = g.Sum(Function(o) o.Accuracy), PostTotal = g.Count
            Where AccTot >= 1000
        ).ToList

        'elimino la lista precedende
        DB.TAB.Categorize_Account.BY.Categorize_Account.Account_Id(CurrAccount.Id, mDB.Categorize.Type.TEMP).Delete.ExecuteCommand()

        'inserisco gli account nel db
        DB.InitMultiRowsInsert()
        For Each Acc In Accounts
            DB.InsertRowOnSubmit(New mDB.Categorize_Account With {
                .Account_Id = CurrAccount.Id,
                .Categorize_Id = mDB.Categorize.Type.TEMP,
                .Account_Categozie_Id = Acc.id,
                .Post_Total = Acc.PostTotal,
                .Accuracy = Acc.AccTot
            })
        Next
        DB.SubmitChanges()

        Me.LblUserFound.Text = String.Format("Trovati {0} utenti", Accounts.Count)

        SetDataListAccount()


        Me.UpPCerca.Update()
        ' Return f.Result.Text1

    End Sub
    Public Sub SetDataListAccount()


        Dim DtAcc = SortGridLocations.SortQuery(
            DB.TAB.Account.
            REL.Account.Account_Add_Info.
            REL.Account.Categorize_Account.
            BY.Categorize_Account.Account_Id(CurrAccount.Id, mDB.Categorize.Type.TEMP)
        ).
        ToDataTable()

        Me.GridUsers.PageSize = 30
        UI.Grid.Bind(Me.GridUsers, DtAcc)

    End Sub
    Public Function IdsToWord(s As String, Dic As SimilText.Dictionary, WordSearch As Dictionary(Of String, String)) As String

        Dim ids = s.Split({"_"}, StringSplitOptions.RemoveEmptyEntries)

        Dim w = ""

        For ct = 0 To ids.Length - 1
            If ct > 0 Then w &= " + "
            Dim word = Dic.WordsList(ids(ct)).Word
            If WordSearch.ContainsKey(word) Then
                w &= "<b>" & word & "</b>"
            Else
                w &= word
            End If

        Next

        Return w


    End Function
    'Private Sub Clustered(ids() As UInteger, n As Integer)


    '    Dim i = ids.Where(Function(o) dic.WordsList(o).IsHashtag).Distinct.OrderBy(Function(o) o).ToArray


    '    Select Case n
    '        Case 1
    '            For ct3 = 1 To i.Length
    '                IncKey({i(ct3 - 1)})
    '            Next
    '        Case 2
    '            For ct2 = 1 To i.Length - 1
    '                For ct3 = ct2 + 1 To i.Length
    '                    IncKey({i(ct2 - 1), i(ct3 - 1)})

    '                Next
    '            Next

    '        Case 3

    '            For ct1 = 1 To i.Length - 2
    '                For ct2 = ct1 + 1 To i.Length - 1
    '                    For ct3 = ct2 + 1 To i.Length
    '                        IncKey({i(ct1 - 1), i(ct2 - 1), i(ct3 - 1)})
    '                    Next
    '                Next
    '            Next

    '    End Select




    'End Sub

    'Dim Clusters As New Dictionary(Of String, Integer)
    'Private Sub IncKey(ids() As UInteger)

    '    Dim id = ""
    '    For ct = 0 To ids.Length - 1
    '        id &= ids(ct) & "_"
    '    Next

    '    If Clusters.ContainsKey(id) Then
    '        Clusters(id) += 1
    '    Else
    '        Clusters.Add(id, 1)

    '    End If



    'End Sub

    Protected Sub GridUsers_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridUsers.PageIndexChanging

        Me.GridUsers.PageIndex = e.NewPageIndex

        Me.SetDataListAccount()

    End Sub
    Protected Sub GridUsers_Sorting(sender As Object, e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridUsers.Sorting

        Me.SortGridLocations.Sort(e.SortExpression)

        Me.SetDataListAccount()


    End Sub

    'Protected Sub Btncerca_Click(sender As Object, e As System.EventArgs) Handles Btncerca.Click

    '    SetDataSearch()

    'End Sub

    'Protected Sub RptHasttag_ItemCommand(source As Object, e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles RptHasttag.ItemCommand

    '    Me.Txtpost.Text &= " " & e.CommandArgument

    '    Me.UpPtext.Update()

    'End Sub
#End Region



End Class
